﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CtpGglGeo.Core
{
    /// <summary>
    /// Class for accessing Google Geoder API
    /// Author: Jeff
    /// Date: 08/0252010
    /// Refer: http://code.google.com/apis/maps/documentation/geocoding/index.html
    /// </summary>
    public class GoogleGeoAPI: WebUtil
    {
        /// <summary>
        /// key for google language translate
        /// </summary>
        public static String GOOGLE_GEOCODING_API_URL = "http://maps.google.com/maps/api/geocode/";

        public static String SendRequest(SpotInfo sinfo, String format)
        {
            String query = GOOGLE_GEOCODING_API_URL + format;
            if (sinfo.LatLng == SpotLatLng.NOT_DEFINE_LAT_LNG)
                query += "?address=" + sinfo.Address;
            else
                query += "?latlng=" + sinfo.LatLng.ToString();
            query += "&sensor=" + ((sinfo.Sensor) ? "true" : "false");
            if (!String.Empty.Equals(sinfo.Language))
                query += "&language=" + sinfo.Language;
            return WebRequest(query);
        }

    }

    /// <summary>
    /// Contains geographic coordinates and address
    /// </summary>
    public class SpotInfo
    {
        public SpotLatLng LatLng;
        public String Address;
        public bool Sensor;
        public String Language;

        public SpotInfo()
        {
            this.LatLng = SpotLatLng.NOT_DEFINE_LAT_LNG;
            this.Address = String.Empty;
            this.Language = String.Empty;
        }
    }

    /// <summary>
    /// Contains geographic coordinates
    /// </summary>
    public class SpotLatLng : ICloneable
    {
        public Double Lat, Lng;
        public SpotLatLng(double Lng, double Lat)
        {
            if (Lng == 0)
                Lng = -999;
            if (Lat == 0)
                Lat = -999;
            this.Lat = Lat;
            this.Lng = Lng;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (!(obj is SpotLatLng))
                return false;
            SpotLatLng sll = (SpotLatLng)obj;
            return (sll.Lat == Lat && sll.Lng == Lng);
        }

        public override int GetHashCode()
        {
            int prime = 31;
            int result = 1;
            double temp;
            temp = Lat;
            result = prime * result + (int)(temp);
            temp = Lng;
            result = prime * result + (int)(temp);
            return result;
        }

        public override string ToString()
        {
            return Lat + "," + Lng;
        }

        public static SpotLatLng NOT_DEFINE_LAT_LNG = new SpotLatLng(-999, -999);

        #region ICloneable Members

        public object Clone()
        {
            return new SpotLatLng(this.Lng, this.Lat);
        }

        #endregion
    }
}
